# coding=utf-8
import sys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
import logging
import random
import re
import subprocess
from bs4 import BeautifulSoup
from lxml import etree
import requests
from random import sample
import lxml
from PIL import Image
from selenium.webdriver import ActionChains
import urllib
import ssl
import datetime
import json
import math

ssl._create_default_https_context = ssl._create_unverified_context

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')


class crawl_baidu:

    def __init__(self):

        # 头条登录页面
        # self.login_url = "https://e.oceanengine.com/account/page/login/account?from=https%3A%2F%2Fe.oceanengine.com%2Faccount%2Fpage%2Fhome%3Flocale%3Dzh"
        self.login_url = "https://cas.baidu.com/?tpl=www2&fromu=http%3A%2F%2Fwww2.baidu.com%2Fcommon%2Fappinit.ajax"
        self.today_date = datetime.datetime.now().strftime('%Y-%m-%d')
        self.name = "user"
        self.password = "pwd"
        # self.driver = webdriver.PhantomJS(executable_path=r'/home/postgres/haoyin/phantomjs_dir/phantomjs-2.1.1-linux-x86_64/bin/phantomjs')
        self.chrome_options = webdriver.ChromeOptions()
        self.chrome_options.add_argument('--headless')
        # self.chrome_options.add_experimental_option('excludeSwitches', ['enable-debug'])
        # self.chrome_options.add_argument('log-level=3')
        # self.chrome_options.add_argument('Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;')
        self.driver = webdriver.Chrome(executable_path="/bin/chromedriver",
                                       chrome_options=self.chrome_options)

    def login(self):

        self.driver.get(self.login_url)
        time.sleep(round(random.uniform(0.71, 1.59), 1))
        #账号登录

        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="app"]/div[1]/div[2]/div[2]/div[1]/span')).click()
        # 用户名
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="uc-common-account"]')).send_keys("%s" % self.name)
        # 密码
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="ucsl-password-edit"]')).send_keys("%s" % self.password)
        # 登陆
        WebDriverWait(self.driver, 5).until(
            lambda driver: self.driver.find_element_by_xpath(
                '//*[@id="submit-form"]')).click()
        time.sleep(round(random.uniform(0.51, 1.39), 1))
        self.driver.save_screenshot('screenshot.png')
        print (self.driver.current_url)
        self.driver.quit()


    def exec_login_crawl(self):

        try:
            self.login()
        except Exception as e:
            print (e)
            time.sleep(round(random.uniform(0.71, 1.59), 1))
            self.login()


if __name__ == '__main__':
    ct = crawl_baidu()
    ct.exec_login_crawl()

